package com.supermap.dataservice.server.gwfs;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.thrid.gwfs.util.GWFSWsdlServiceFactory;
import com.supermap.dataservice.thrid.gwfs.wdsl.ResponseData;
import com.supermap.dubbo.service.GWFSDubboService;

import java.rmi.RemoteException;

/**
 * @author xiaolu
 * @date 2018/6/11 下午4:40
 */
@Service(version = "1.0.0")
public class GWFSDubboServiceImpl implements GWFSDubboService {
    @Override
    public JSONObject getGridDataWithCoordinate(JSONObject requestJSON) {
        JSONObject param = assistElementParam(requestJSON);
        param.put("coordinate",requestJSON.get("coordinate"));

        ResponseData data;
        try {
            data = GWFSWsdlServiceFactory.getCXFDataSharePort().QXTA_GWFS_getGridDataWithCoordinate(param.toJSONString());
        } catch (RemoteException e) {
            return assistMsg(404, null);
        }
        return assistMsg(200, data.getData());
    }

    @Override
    public JSONObject getWeatherPhenomenaInGridWithCoordinate(JSONObject requestJSON) {
        JSONObject param = assistWeatherParam(requestJSON);
        param.put("coordinate",requestJSON.get("coordinate"));
        ResponseData data;
        try {
            data = GWFSWsdlServiceFactory.getCXFDataSharePort().QXTA_GWFS_getWeatherPhenomenaInGridWithCoordinate(param.toJSONString());
        } catch (RemoteException e) {
            return assistMsg(404, null);
        }
        return assistMsg(200, data.getData());
    }

    /**
     * 基础参数验证
     * @param requestJSON 参数
     * @return
     */
    private JSONObject assistParam(JSONObject requestJSON) {
        JSONObject param = new JSONObject();
        param.put("userName",requestJSON.get("userName"));
        param.put("password",requestJSON.get("password"));
        param.put("origin",requestJSON.get("origin"));
        param.put("sequenceValue",requestJSON.get("sequenceValue"));
        param.put("forecastDate",requestJSON.get("forecastDate"));
        param.put("forecastValidStart",requestJSON.get("forecastValidStart"));
        param.put("forecastValidEnd",requestJSON.get("forecastValidEnd"));
        param.put("forecastFileVersion",requestJSON.get("forecastFileVersion"));
        return param;
    }

    /**
     * 天气现象参数验证
     * @param requestJSON 参数
     * @return
     */
    private JSONObject assistWeatherParam(JSONObject requestJSON) {
        JSONObject param = assistParam(requestJSON);
        param.put("interval",requestJSON.get("interval"));
        return param;
    }

    /**
     * 气象要素参数验证
     * @param requestJSON 参数
     * @return
     */
    private JSONObject assistElementParam(JSONObject requestJSON) {
        JSONObject param = assistParam(requestJSON);
        param.put("element",requestJSON.get("element"));
        return param;
    }

    /**
     * 错误信息
     *
     * @param errorCode 错误代码
     * @return 查询结果
     */
    private JSONObject assistMsg(int errorCode, Object data) {
        JSONObject msg = new JSONObject();
        msg.put("code", errorCode);
        if (errorCode == 404) {
            msg.put("msg", "SERVICE GET AN EXCEPTION");
        } else {
            msg.put("msg", "SERVICE GET SUCCESS");
        }
        msg.put("data", data);
        return msg;
    }
}
